<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织结构" name="first" class="tab-pane">

              <el-row type="flex" justify="space-between" align="middle" class="department-header">
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">人力资源管理系统</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown>
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="add()">添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>

              <!-- 在这里设置, 树形控件 -->
              <el-tree class="departments-tree" :data="treeData" :props="defaultProps" :default-expand-all="true">
                <!-- 默认插槽  并且使用内部的数据进行自定义模板的渲染, scope.data 所以直接key解构赋值变量也是data(对应数据对象)-->
                <template slot-scope="{ data }">
                  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>

                    <!-- 功能操作区域 -->
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="add(data)">添加子部门</el-dropdown-item>
                              <el-dropdown-item @click.native="edit(data)">编辑部门</el-dropdown-item>
                              <el-dropdown-item v-if="data && !data.children" @click.native="del(data)">删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>

                  </el-row>
                </template>
              </el-tree>

            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
      <!-- 新增-子部门-弹框 -->
      <depart-dialog
        ref="departDialogRef"
        :dialog-visible.sync="showDepartDialog"
        :employee-simple-list="employeeSimpleList"
        :origin-list="originList"
        :is-edit="isEdit"
        :current-depart-id="clickDepartId"
        @addDepartEV="addDepartFn"
      />
    </div>
  </div>
</template>

<script>
import { departmentsListAPI, transTree, getEmployeeSimpleAPI, addDepartmentsAPI, getDepartDetailAPI,
  updateDepartmentsAPI, delDepartmentAPI } from '@/api'
import DepartDialog from './components/departDialog.vue'

export default {
  name: 'Department',
  components: {
    DepartDialog
  },
  data() {
    return {
      activeName: 'first',
      // 树形控件数据
      treeData: [
        {
          departName: '总裁办',
          name: '孙财'
        },
        {
          departName: '行政部',
          name: '罗晓晓'
        },
        {
          departName: '人事部',
          name: '安小晴',
          children: [
            {
              departName: '财务核算部'
            }
          ]
        },
        {
          departName: '财务部',
          name: '蔡徐坤',
          children: [
            {
              departName: '财务核算部'
            },
            {
              departName: '税务管理部'
            },
            {
              departName: '薪资管理部'
            }
          ]
        },
        {
          departName: '技术部',
          name: '吴亦凡',
          children: [
            {
              departName: '前端技术部'
            },
            {
              departName: '后端技术部'
            }
          ]
        },
        {
          departName: '运营部',
          name: '李易峰'
        },
        {
          departName: '市场部',
          name: '武高丽',
          children: [
            {
              departName: '市场部1'
            },
            {
              departName: '市场部2'
            }
          ]
        }
      ],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      originList: [],
      showDepartDialog: false,
      employeeSimpleList: [],
      clickDepartId: '',
      isEdit: false
    }
  },
  created() {
    this.getDepartmentsList()
    this.getEmployeeSimpleList()
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },

    // 获取部门数据
    async getDepartmentsList() {
      const res = await departmentsListAPI()
      this.originList = res.data.depts.map(item => (
        {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid // 下面使用
        }
      ))
      this.treeData = transTree(res.data.depts, '')
    },

    // 获取员工简单列表
    async getEmployeeSimpleList() {
      const res = await getEmployeeSimpleAPI()
      this.employeeSimpleList = res.data
    },

    // 添加部门
    async addDepartFn(formObj) {
      let res = null
      if (this.isEdit) {
        res = await updateDepartmentsAPI(formObj)
      } else {
        formObj.pid = this.clickDepartId
        res = await addDepartmentsAPI(formObj)
      }
      if (res.success) {
        this.$message.success(res.message)
      } else {
        this.$message.error(res.message)
      }
      this.getDepartmentsList()
    },

    // 正文部分-右侧的添加子部门
    add(data) {
      if (data === undefined) {
        this.clickDepartId = ''
      } else {
        this.clickDepartId = data.id
      }
      this.isEdit = false
      this.showDepartDialog = true
    },
    // 编辑子部分
    async edit(data) {
      this.clickDepartId = data.id
      this.isEdit = true
      const res = await getDepartDetailAPI(data.id)
      this.$refs.departDialogRef.form = res.data
      this.showDepartDialog = true
    },
    // 删除部分
    del(data) {
      this.$confirm('确定永久删除改部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartmentAPI(data.id)
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      }).finally(() => {
        this.getDepartmentsList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }

  .departments-tree {
    margin: 12px 0px;
  }
}
</style>
